package com.tian.manager.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tian.commonutils.Result;
import com.tian.manager.entity.SysPerson;
import com.tian.manager.entity.vo.PersonQuery;
import com.tian.manager.service.SysPersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tian
 * @since 2020-05-17
 */
@RestController
@RequestMapping("/manager/sysperson")
@CrossOrigin
public class SysPersonController {

    @Autowired
    private SysPersonService sysPersonService;


    /**
     * 人员分页查询
     * @param current
     * @param limit
     * @return
     */
    @GetMapping("/getPerson/{current}/{limit}")
    public Result getAll(@PathVariable Long current, @PathVariable Long limit) {
        Page<SysPerson> page = new Page<>(current,limit);
        sysPersonService.page(page, null);
        Long total = page.getTotal();
        List<SysPerson> records = page.getRecords();
        return Result.ok().data("total", total).data("records", records);
    }

    /**
     * 更改人员信息
     * @param sysPerson
     * @return
     */
    @PostMapping("/updatePerson")
    public Result updatePerson(@RequestBody SysPerson sysPerson) {
        sysPersonService.updateById(sysPerson);
        return Result.ok();
    }

    /**
     * 根据id和name查询人员信息
     * @param current
     * @param limit
     * @param personQuery
     * @return
     */
    @PostMapping("/getPersonByNameAndId/{current}/{limit}")
    public Result getDetailedPerson(@PathVariable Long current,
                                    @PathVariable Long limit,
                                    @RequestBody PersonQuery personQuery) {
        System.out.println("personQuery"+personQuery);
        Page<SysPerson> page = new Page<>(current,limit);
        QueryWrapper<SysPerson> wrapper = new QueryWrapper<>();
        String id = personQuery.getId();
        String name = personQuery.getName();
        if (!StringUtils.isEmpty(name)) {
            wrapper.eq("person_name", name);
        }
        if (!StringUtils.isEmpty(id)) {
            wrapper.eq("person_inner_id", id);
        }
        sysPersonService.page(page, wrapper);
        long total = page.getTotal();
        List<SysPerson> records = page.getRecords();
        return Result.ok().data("total", total).data("records", records);
    }


}

